package com.example.comm.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.comm.Adapter.HomeAdapter;
import com.example.comm.Bean.Post;
import com.example.comm.Bean.User;
import com.example.comm.R;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class FragmentHome extends Fragment {
    private RecyclerView rv;
    private SwipeRefreshLayout srlayout;
    private TextView HelloHome;
    private TextView username,ok;


    List<Post> data;
    private HomeAdapter homeAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragmenthome,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //逻辑处理
        initView(view);


        Bmob.initialize(getActivity(),"bf3767bf4ff13d8b86eca605eb177e0a");


        //初始刷新
        Refresh();

    }


    private void Refresh() {
        //从后台查询信息
        BmobQuery<Post> Po=new BmobQuery<Post>();
        Po.order("-createdAt");
        Po.setLimit(1000);
        Po.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                srlayout.setRefreshing(false);
                if(e==null){
                    data=list;
                    homeAdapter = new HomeAdapter(getActivity(),data);
                    rv.setLayoutManager(new LinearLayoutManager(getActivity()));
                    rv.setAdapter(homeAdapter);
                }else{
                    srlayout.setRefreshing(false);
                    Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView(View view) {
        rv=view.findViewById(R.id.recyclerview);
        srlayout=view.findViewById(R.id.swipe);
        HelloHome=view.findViewById(R.id.HelloHome);
        ok=view.findViewById(R.id.hy);
        username=view.findViewById(R.id.user);
        srlayout.setColorSchemeResources(R.color.purple_200);
        //监听刷新
        srlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh();
            }
        });
        //user加载
        BmobUser bu=BmobUser.getCurrentUser(User.class);
        String userid=bu.getObjectId();
        BmobQuery<User> us=new BmobQuery<>();
        us.getObject(userid, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if(e==null){
                    username.setText(user.getUsername());
                }else{
                    username.setText(" ");
                    ok.setText(" ");
                }
            }
        });
    }
}
